<cfset hibernate = request.hibernateService />
<cfsavecontent variable="availableActions">
	<hr />
	<h3>Action List</h3>
<cfoutput>
	<a href="?action=generateall">Generate All</a> | 
	<a href="?init=1">Init</a> |
	<a href="?action=entitylist">Entity List</a> |
	<a href="?action=test&init=1">Test</a>

	<br /> 
</cfoutput>
</cfsavecontent>
<cfoutput>#availableActions#</cfoutput>
<cfparam name="action" default="">

<cfswitch expression="#action#">
  <cfcase value="test">
		<cfset test = createObject("component","HibernateTest").runAllTests(hibernate) />
		TESTED
  </cfcase>

  <cfcase value="entitylist">
		<cfset metaData = hibernate.getAllEntityMetaData() />
		<cfoutput>
		<cfloop list="#hibernate.getEntityList()#" index="table"> |
			<a href="?action=list&object=#table#">#table#</a> |
			<a href="?action=save&object=#table#">Save</a> |
			<a href="?action=edit&object=#table#&#metaData[table]["KeyColumnNames"][1]#=0">New</a> |
			<a href="?action=generateentity&object=#table#">Generate Entity</a>
			<br />
		</cfloop>
		</cfoutput>
  </cfcase>

  <cfcase value="list">
		<cfset metaData = hibernate.getAllEntityMetaData() />
		<cfset metaData = metaData[url.object] />
		<cfset result = hibernate.query("from #url.object#") />
		<cfoutput>
		   <cfloop from="1" to="#arrayLen(result)#" index="e">
		   		<cfset entity = result[e]>
		   		<cfset properties = structKeyList(entity) />
		   		<cfdump var="#entity#" />
		   		<cfloop list="#properties#" index="prop">
			   		<a href="?action=edit&object=#url.object#&#metaData["IdentifierPropertyName"]#=#entity[metaData["IdentifierPropertyName"]]#">
					#prop# = <cftry>#entity[prop]#,<cfcatch>NULL,</cfcatch></cftry>
							</a>
					</cfloop><br />
<!---
 		      <a href="?action=event.form&eventID=#i.getEventId()#">#i.getEventname()# - #dateFormat(i.getEventDate())#</a><br />
		      <cfset personIterator = i.getParticipants().iterator() />
				   <cfloop condition="#personIterator.hasNext()#">
					   <cfset p = personIterator.next() />
				     &nbsp; &nbsp; #p.getFirstname()# - #p.getLastname()#<br />
				   </cfloop>
				   <br />
 --->
		   </cfloop>
		</cfoutput>
		
  </cfcase>

  <cfcase value="edit">
		<cfset metaData = hibernate.getAllEntityMetaData() />
		<cfset metaData = metaData[url.object] />
		<cfparam name="dakey.#metaData["KeyColumnNames"][1]#" default="#url[metaData["KeyColumnNames"][1]]#">
		<cfset result = hibernate.load("#url.object#",dakey[metaData["KeyColumnNames"][1]]) />
		<cfset properties = structKeyList(result) />
		<cfdump var="#result#" />
		<cfoutput>
			#dakey[metaData["KeyColumnNames"][1]]#
			<form action="?action=save&object=#url.object#" method="post">
			<cfloop list="#properties#" index="p">
			  #p# (#p#)<input type="text" name="#p#" value="#result[p]#"><br />
			</cfloop>
			<input type="submit" />
			</form>
		</cfoutput>		
  </cfcase>

  <cfcase value="save">
		<cfset result = createObject("component","hibernate.#url.object#").init(form) />
		<cfset hibernate.save("#url.object#",result.getHashMap()) />
		<cfset enMeta = getMetaData(result) />
		<cfset properties = enMeta["properties"] />
		<cfoutput>
			<form action="?action=save&object=#url.object#" method="post">
			<cfloop from="1" to="#arrayLen(properties)#" index="p">
			  #properties[p]["name"]# (#properties[p]["name"]#)<input type="text" name="#properties[p]["name"]#" value="#properties[p]["default"]#"><br />
			</cfloop>
			<input type="submit" />
			</form>
		</cfoutput>		
  </cfcase>


  <cfcase value="generateentity">
		<cfset result = hibernate.generateEntities(url.object) />
		Generated entity
		<cfoutput>
		</cfoutput>		
  </cfcase>

  <cfcase value="generateall">
		<cfset result = hibernate.generateEntities() />
		Generated entities
		<cfoutput>
		</cfoutput>		
  </cfcase>

</cfswitch>